package com.heo.sso.server.controller;

import com.heo.sso.server.dto.TicketGrantingTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 刘康
 * @Date 2020-11-03 10:07
 * @Description
 * @Revision
 **/
@RestController
@RequestMapping("/")
@Slf4j
public class SSOController {


    /**
     * 验证ticket， ticket验证一次便失效
     *
     * @param ticket
     * @return
     */
    @PostMapping("/auth")
    public ResponseEntity validTicket(String ticket, String service) {
        if (ticket == null) {
            log.error("ticket为空");
            log.info("~~~~~~~~~~~~~~~");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("ticket为空");
        }
        if (service == null) {
            log.error("service为空为空");
            log.info("~~~~~~~~~~~~~~~");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("service为空");
        }
        TicketGrantingTicket ticketGrantingTicket = TicketGrantingTicket.validTicket(ticket, service);
        if (ticketGrantingTicket != null) {
            log.info("ticket验证成功");
            log.info("~~~~~~~~~~~~~~~");
            return ResponseEntity.ok().body(ticketGrantingTicket.getData());
        } else {
            log.error("无效的ticket");
            log.info("~~~~~~~~~~~~~~~");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("无效的ticket");
        }
    }

}
